package com.yc.servlet;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yc.bean.OrderBean;
import com.yc.bean.OrderDetailBean;
import com.yc.bean.UserBean;
import com.yc.dao.OrderDao;
import com.yc.dao.OrderDetailDao;
import com.yc.dao.UserDao;
import com.yc.model.JsonModel;
import com.yc.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends BaseServlet {

    public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");


        JsonModel jm=new JsonModel();
        List<UserBean> userBeans = UserDao.find(username, pwd);
        System.out.println(userBeans);
        if (!userBeans.isEmpty()){
            jm.setCode(1);
            jm.setObj(userBeans);
        }else {
            jm.setCode(0);
        }
        super.writeJson(jm,resp);
    }
    public void register(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String nickname = req.getParameter("nickname");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");

        JsonModel jm=new JsonModel();
        int rs = UserDao.insert(nickname,username, pwd);
        if (rs>0){
            jm.setCode(1);
        }else {
            jm.setCode(0);
        }
        super.writeJson(jm,resp);
    }
}
